//
//  CarInfoView.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/10/30.
//

import SwiftUI

enum EnumCallState {
  case idle
  case calling
}

struct CallSpecCarView: View {
  var car: CarInfo = ViewModel.shared.car!
  @State var status: EnumCallState = .idle
  @Binding var showCallWindow: Bool

  var parent : ContentView?

  var body: some View {
    VStack {
      HStack {
        HStack {
          Image(systemName: "car.fill")
          Text("\(car.carNo)")
        }
        Spacer()
        HStack {
          Image(systemName: "person.text.rectangle")
          Text("\(car.driverMobile)")
          Button {
            print("call phone number:\(car.driverMobile)")
            guard let number = URL(string: "tel://" + car.driverMobile) else { return }
            UIApplication.shared.open(number)
          } label: {
            Image(systemName: "phone.circle.fill")
              .resizable()
              .scaledToFit()
              .frame(width: 32,height: 32)
              .foregroundColor(.green)
          }
        }

      }.padding()

      if status == .idle {
        HStack {
          Button {
            print("call taxi......")
            self.status = .calling
            self.showCallWindow.toggle()
            parent?.callSpecCar(driverId: car.id)
          } label: {
            HStack {
              Image(systemName: "phone.arrow.up.right")
              Text("呼叫出租车")
            }.padding().background(Color(hex: "FF862F8D"))
              .cornerRadius(8)
          }

          Button {
            print("cancel call taxi, close window......")
            self.status = .idle
            self.showCallWindow.toggle()
          } label: {
            HStack {
              Image(systemName: "xmark.circle.fill")
              Text("关闭")
            }.padding().background(.red)
              .cornerRadius(8)
          }
        }.foregroundColor(.white)
      } else {
        VStack {
          HStack {
            Image(systemName: "phone.connection.fill")
              .resizable()
              .scaledToFit()
              .frame(width: 48,height: 48)
            Text("正在叫车......").font(.system(size: 24)).padding()
          }.foregroundColor(.green)
            .padding().padding(.bottom,10)

          Button {
            print("cancel calling.")
            self.status = .idle
          } label: {
            HStack {
              Image(systemName: "phone.down")
              Text("取消呼叫")
            }.padding().background(.red).foregroundColor(.white)
              .cornerRadius(8)
          }
        }
      }
    }
    .font(.system(size: 20))
    .padding()
    .background(.white)
      .cornerRadius(8)
      .foregroundColor(.black)
      .overlay(
        RoundedRectangle(cornerRadius: 8, style: .continuous)
          .stroke(Color.gray, lineWidth: 1).shadow(radius: 5))
      .offset(y:-20)
  }
}

struct CarInfoView_Previews: PreviewProvider {
  static var previews: some View {
    CallSpecCarView(showCallWindow: .constant(true))
  }
}
